package DBFunction;
import DTO.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.log4j.Logger;

public class GroupManager {
	public static Logger logger = Logger.getLogger("BuddyTrackingServer");
	public static void insert(String inputGroup){
		String sql="INSERT into buddytracking.group (GroupName) VALUES('"+inputGroup+"')";
		DB.connectDB.Update(sql);
		logger.info("GroupManager.insert: Group "+inputGroup);
	}
	public static void rename(String oldName, String newName){
		String sql="UPDATE buddytracking.group set GroupName='" + newName
		+ "' where GroupName='" + oldName + "'";
		DB.connectDB.Update(sql);
		logger.info("GroupManager.rename: "+oldName+" changed to "+newName);
	}
	public static String getGroupId(String groupName) throws SQLException{
		String groupId="";
		String sql="SELECT GroupId FROM buddytracking.group " +
				"WHERE GroupName='"+groupName+"'";
		ResultSet resultSet = DB.connectDB.ExecuteQuery(sql);
		while (resultSet.next()) {
			groupId = resultSet.getString("GroupId");
		}
		return groupId;
	}
	public static void delete(String groupName){
		String sql="DELETE FROM buddytracking.group WHERE GroupName='"+groupName+"'";
		DB.connectDB.Update(sql);
		logger.info("GroupManager.delete "+groupName);
	}
	public static ArrayList<String> getGroupList()throws  SQLException{
		logger.info("GroupManager.getGroupList.begin");
		ArrayList<String> groupList = new ArrayList();
		String sql="SELECT * FROM buddytracking.group WHERE groupid>0";
		ResultSet result = DB.connectDB.ExecuteQuery(sql);
		while (result.next()) {
			String selectedGroup = new String();
			selectedGroup = result.getString("GroupName");
			groupList.add(selectedGroup);
			logger.info("GroupManager.getGroupList: Group "+selectedGroup+" added.");
		}
		logger.info("GroupManager.getGroupList: Total Groups is "+groupList.size());
		logger.info("GroupManager.getGroupList.end");
		return groupList;
	}
}
